<template>
  <div>
  <div class="shopcart">
    <div class="content" @click="toglleList">
      <div class="content-left">
        <div class="logo-wrapper">
          <div class="logo" :class="{'highlight':totalCount > 0}">
            <i class="icon-shopping_cart" :class="{'highlight':totalCount > 0}">☞</i>
          </div>
          <div class="num" v-show="totalCount > 0">{{totalCount}}</div>
        </div>
        <div class="price" :class="{'highlight':totalPrice > 0}">￥{{totalPrice}}元</div>
        <div class="desc">另需配送费￥{{this.sellers.deliveryPrice}}元</div>
      </div>
      <div class="content-right" @click.stop.prevent="pay">
        <div class="pay" :class="payClass">
          {{payDesc}}
        </div>
      </div>
    </div>
    <div class="ball-container" v-for="(ball, index) in balls" :key="index">
      <transition
        @before-enter="beforeDrop"
        @enter="dropping"
        @after-enter="afterDrop">
        <div class="ball" v-show="ball.show">
          <div class="inner inner-hook"></div>
        </div>
      </transition>
    </div>
    <transition name="fold">
    <div class="shopcart-list" v-show="listShow">
      <div class="list-header">
        <h1 class="title">购物车</h1>
        <span class="empty" @click="empty">清空</span>
      </div>
      <div class="list-content" ref="listcontent">
        <ul>
          <li class="food" v-for="food in selectFoods" :key="food.index">
            <span class="name">{{food.name}}</span>
            <div class="price">
              <span>￥{{food.price * food.count}}</span>
            </div>
            <div class="cartcontrol-wrapper">
              <cartcontrol :food="food"></cartcontrol>
            </div>
          </li>
        </ul>
      </div>
    </div>
    </transition>
  </div>
  <transition name="fade">
    <div class="list-mask" v-show="listShow" @click="toglleList"></div>
  </transition>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import Cartcontrol from './Cartcontrol'
import BScroll from 'better-scroll'
export default {
  name: 'Shopcart',
  components: {
    Cartcontrol
  },
  data () {
    return {
      balls: [{
        show: false,
        num: 1
      }, {
        show: false,
        num: 2
      }, {
        show: false,
        num: 3
      }, {
        show: false,
        num: 4
      }, {
        show: false,
        num: 5
      }],
      dropBalls: [],
      count: '',
      fold: false
    }
  },
  props: {
    selectFoods: {
      type: Array,
      default () {
        return []
      }
    }
  },
  computed: {
    ...mapState({sellers: 'sellers'}),
    totalPrice () {
      let total = 0
      this.selectFoods.forEach((food) => {
        total += food.price * food.count
      })
      return total
    },
    totalCount () {
      let count = 0
      this.selectFoods.forEach((food) => {
        count += food.count
      })
      return count
    },
    payDesc () {
      if (this.totalPrice === 0) {
        return `￥${this.sellers.minPrice}元起送`
      } else if (this.totalPrice < this.sellers.minPrice) {
        let diff = this.sellers.minPrice - this.totalPrice
        return `还差￥${diff}元起送`
      } else {
        return `去结算`
      }
    },
    payClass () {
      if (this.totalPrice < this.minPrice) {
        return 'not-enough'
      } else {
        return 'enough'
      }
    },
    /* eslint-disable */
    listShow () {
      if (this.totalCount && this.fold) {
        this.$nextTick(() => {
          if (!this.scroll) {
            this.scroll = new BScroll(this.$refs.listcontent, {
              click: true
            })
          } else {
            this.scroll.refresh()
          }
        })
        return true
      }
      // 当totalCount === 0时将fold也设置成false
      this.fold = false
      return false
    }
  },
  methods: {
    drop (el) {
      for (let i = 0; i < this.balls.length; i++) {
        let ball = this.balls[i]
        if (!ball.show) {
          ball.show = true
          ball.el = el
          this.dropBalls.push(ball)
          this.count = i
          return
        }
      }
    },
    beforeDrop (el) {
      let ball = this.balls[this.count]
      let rect = ball.el.getBoundingClientRect()
      let x = rect.left - 32
      let y = -(window.innerHeight - rect.top - 24)
      el.style.webkitTransform = `translate3d(0,${y}px,0)`
      el.style.transform = `translate3d(0,${y}px,0)`
      let inner = el.getElementsByClassName('inner-hook')[0]
      inner.style.webkitTransform = `translate3d(${x}px,0,0)`
      inner.style.transform = `translate3d(${x}px,0,0)`
    },
    dropping (el, done) {
      /* eslint-disable */
      let refresh = el.offsetHeight
      this.$nextTick(() => {
        el.style.webkitTransform = 'translate3d(0,0,0)'
        el.style.transform = 'translate3d(0,0,0)'
        let inner = el.getElementsByClassName('inner-hook')[0]
        inner.style.webkitTransform = 'translate3d(0,0,0)'
        inner.style.transform = 'translate3d(0,0,0)'
        el.addEventListener('transitionend', done)
      })
    },
    afterDrop (el) {
      let ball = this.dropBalls.shift()
      if (ball) {
        ball.show = false
        el.style.display = 'none'
      }
    },
    toglleList () {
      if(!this.totalCount) {
        //如果totalCount=0就不允许点击（返回空）
        return
      }
      this.fold = !this.fold
    },
    empty () {
      this.selectFoods.forEach((food) => {
        food.count = 0
      })
    },
    pay () {
      if(this.totalPrice < this.sellers.minPrice) {
        return
      }
      window.alert(`支付${this.totalPrice}`)
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '../../assets/styles/maxin.styl'
  .shopcart
    position fixed
    left 0
    bottom 0
    z-index 49
    width 100%
    height .96rem
    .content
      display flex
      background #141d27
      font-size 0
      position absolute 
      left 0
      right 0
      z-index 80
      .content-left
        flex 1
        .logo-wrapper
          display inline-block
          position relative
          top -.1rem
          margin 0 .24rem
          padding .12rem
          width 1.12rem
          height 1.12rem
          box-sizing border-box
          vertical-align top
          border-radius 50%
          background #141d27
          .logo
            width 100%
            height 100%
            border-radius 50%
            background #2b343c
            text-align center
            &.highlight
              background rgb(0,160,220)
            .icon-shopping_cart
              line-height .88rem
              font-size .48rem
              color #80858a
              &.highlight
                color #fff
          .num
            position absolute
            top 0
            right 0
            width .48rem
            height .32rem
            line-height .32rem
            text-align center
            border-radius .32rem
            font-size .18rem
            font-weight 700
            color #fff
            background rgb(240,20,20)
            box-shadow 0 4px 8px 0 rgba(0,0,0,0.4)
        .price
          display inline-block
          vertical-align top
          line-height .48rem
          margin-top .24rem
          color rgba(255,255,255,0.4)
          font-size .32rem
          font-weight 700
          padding-right .24rem
          box-sizing border-box
          border-right 1px solid rgba(255,255,255,0.1)
          &.highlight
            color #fff
        .desc
          display inline-block
          vertical-align top
          line-height .48rem
          margin .24rem 0 0 .24rem
          color rgba(255,255,255,0.4)
          font-size .1rem
      .content-right
        flex 0 0 2.1rem
        width 2.1rem
        .pay
          height .96rem
          line-height .96rem
          text-align center
          font-size .24rem
          color rgba(255,255,255,0.4)
          font-weight 700
          background #2b333b
          &.not-enough
            background #2b333b
          &.enough
            background #00b43c
            color #fff
    .ball-container
      .ball
        position: fixed
        left: 32px
        bottom: 24px
        z-index: 100
        transition: all 1.5s cubic-bezier(0.49, -0.29, 0.75, 0.41)
        .inner
          width: 16px
          height: 16px
          border-radius: 50%
          background-color: rgb(0, 160, 220)
          transition: all 1.5s linear
    .shopcart-list
      position fixed
      left 0
      bottom 1rem
      width 100%
      transition all 0.5s
      z-index 50
      &.fold-enter,&.fold-leave-active
        transform translate3d(0,100%,0)
      .list-header
        height .8rem
        line-height .8rem
        padding 0 .36rem
        background #f3f5f7
        border-bottom 1px solid rbga(7,17,27,0.1)
        .title
          float left
          font-size .28rem
          color rgb(7,17,27)
        .empty
          float right
          font-size .24rem
          color rgb(0,160,220)
      .list-content
        padding 0 .36rem 
        max-height 4.34rem
        background #fff
        overflow hidden
        .food
          position relative
          padding .24rem 0
          box-sizing border-box
          border-1px(rgba(7,17,27,0.1))
          .name
            line-height .48rem
            font-size .28rem
            color rgb(7,17,27)
          .price
            position absolute
            right 1.8rem
            bottom .24rem
            line-height .48rem
            font-size .28rem
            font-weight 700
            color rgb(240,20,20)
          .cartcontrol-wrapper
            position absolute
            right 0
            bottom .12rem
  .list-mask
    position fixed
    top 0
    left 0
    width 100%
    height 100%
    z-index 40
    backdrop-filter blur(10px)
    transition all 0.5s
    background rgba(7,17,27,0.6)
    opacity 1
    &.fade-enter, &fade-leave
      opacity 0
      background rgba(7,17,27,0)
</style>
